package org.chen.common.redis;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/*
redis相关工具类
 */
@Component
public class RedisCache {

    @Autowired
    public RedisTemplate redisTemplate;
    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value)
    {
        redisTemplate.opsForValue().set(key, value);
    }
    /**
     * 在redis中存入
     * @param key 键值
     * @param value 值
     * @param timeout 过期时间
     * @param timeUnit 过期时间单位
     * @param <T>
     */
    public <T> void setCacheObject(final String key, final T value,final Integer timeout,final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    public <T> T getCacheObject(final String key) {
        return (T) redisTemplate.opsForValue().get(key);
    }
    /**
     * 删除单个对象
     *
     * @param key
     */
    public boolean deleteObject(final String key)
    {
        return redisTemplate.delete(key);
    }

}
